<template>
  <el-dialog
    itle="资料提交"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社团名" prop="clubName">
      <span>{{ dataForm.clubName }}</span>
    </el-form-item>
    <el-form-item label="考核资料" prop="url">
        <el-upload
          class="upload-demo"
          action="#"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :http-request="upload"
          multiple
          :limit="1"
          :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        fielList: [],
        dataForm: {
          id: 0,
          clubName: '',
          clubId: '',
          url: '',
          createTime: '',
          status: ''
        },
        dataRule: {
          clubName: [
            { required: true, message: '社团名不能为空', trigger: 'blur' }
          ],
          clubId: [
            { required: true, message: '社团ID不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '考核资料不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '提交时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '审批状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      upload (param) {
        const formData = new FormData()
        formData.append('file', param.file)
        this.$http.post('http://localhost:8080/sys/upload', formData).then(resp => {
          console.log(resp)
          this.dataForm.url = resp.data.msg
        })
      },
      handleRemove (file, fileList) {
        console.log(file, fileList)
      },
      handlePreview (file) {
        console.log(file)
      },
      handleExceed (files, fileList) {
        this.$message.warning(`当前限制选择 1个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      beforeRemove (file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`)
      },
      init (id) {
        this.$http({
          url: this.$http.adornUrl(`/club/clubinfo/getByUser`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.clubId = data.clubInfo.id
            this.dataForm.clubName = data.clubInfo.name
          }
        })
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubexam/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clubName = data.clubExam.clubName
                this.dataForm.clubId = data.clubExam.clubId
                this.dataForm.url = data.clubExam.url
                this.dataForm.createTime = data.clubExam.createTime
                this.dataForm.status = data.clubExam.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubexam/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'clubName': this.dataForm.clubName,
                'clubId': this.dataForm.clubId,
                'url': this.dataForm.url
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.fielList = []
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
